package game;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class GameGUI
{
	private IGUIEventListener listener;
	private Set<String> games;
	private JFrame frame;
	private Board board;
	private JLabel messageLabel = new JLabel("no message now.");
	private JLabel playerLabel = new JLabel(" ");
	private JLabel[] playerColorLabels = new JLabel[]{new JLabel("     "),new JLabel("     ")};
	private JPanel gameSelectPanel;
	private JPanel gamePlayPanel;
	private JPanel gameControllPanel;
	public GameGUI(IGUIEventListener listener, Set<String> games) {
		super();
		this.listener = listener;
		this.games = games;
		board = new Board(listener);
		gameSelectPanel = createGameSelectPanel();
		gameControllPanel = createGameControllPanel();
		gamePlayPanel = createGamePlayPanel();
		frame = createMainFrame();
		for(JLabel lbl : playerColorLabels){
			lbl.setOpaque(true);
		}
	}
	public void Run(){
		frame.setContentPane(gameSelectPanel);
		frame.setBounds(100,100,400,600);
		frame.setVisible(true);
	}
	public void InitBoard(Point size){
		assert board!=null;
		board.init(size);
		frame.setSize((frame.getHeight()*size.x/size.y), frame.getHeight()+gameControllPanel.getHeight()*2);
		//System.out.println(board.getBounds());
	}
	public void SetMessage(String message){
		messageLabel.setText(message);
	}
	public void SetDialogMessage(String message){
		JOptionPane.showMessageDialog(frame, message);
	}
	public void SetBoardEnable(boolean state){
		assert board!=null;
		board.setAllLocationEnable(state);
	}
	public void SetLocationEnable(Point location,boolean state){
		assert board!=null;
		board.setLocationEnable(location, state);
	}
	public void SetCurrentPlayerByID(int id){
		assert id==0 || id==1;
		this.playerLabel.setText("Now is Player" + (id+1) +"'s turn!!");
	}
	public void SetPlayerColor(int playerID,Color color){
		assert playerID==0 || playerID==1;
		playerColorLabels[playerID].setBackground(color);
	}
	private JFrame createMainFrame(){
		JFrame frame = new JFrame("ChessGame");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		return frame;
	}
	private JPanel createGamePlayPanel(){
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		assert board != null && gameControllPanel!=null;
		//System.out.println("1 " + board.getBounds());
		panel.add(board,BorderLayout.CENTER);
		panel.add(gameControllPanel,BorderLayout.NORTH);
		panel.add(createGameMessagePanel(),BorderLayout.SOUTH);
		//System.out.println("2 " + board.getBounds());
		
		return panel;
	}
	private JPanel createGameControllPanel(){
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1,4));
		assert frame!=null;
		panel.add(createPlayerPanel(0));
		panel.add(createPlayerPanel(1));
		panel.add(createTieButton());
		panel.add(createSurrenderButton());
		return panel;
	}
	private JPanel createGameMessagePanel(){
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1,2));
		assert frame!=null;
		panel.add(playerLabel);
		panel.add(messageLabel);
		return panel;
		
	}
	private JPanel createGameSelectPanel(){
		JPanel panel = new JPanel(new FlowLayout());
		assert games != null;
		ActionListener btnListener = createGameBtnListener();
		for(String name : games){
			JButton btn = new JButton(name);
			btn.addActionListener(btnListener);
			panel.add(btn);
		}
		return panel;
	}
	private ActionListener createGameBtnListener(){
		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				listener.OnGameSelected(e.getActionCommand());
				frame.setContentPane(gamePlayPanel);
				frame.setVisible(true);
				board.setVisible(true);
			}
		};
	}
	private JButton createTieButton(){
		JButton btn = new JButton("Tie");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				listener.OnTie();
				Run();
			}
		});
		return btn;
	}
	private JButton createSurrenderButton(){
		JButton btn = new JButton("Surrender");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				SetDialogMessage("Defeat!!");
				Run();
			}
		});
		return btn;
	}
	private JPanel createPlayerPanel(int playerID){
		JPanel panel = new JPanel(new FlowLayout());
		panel.add(playerColorLabels[playerID]);
		panel.add(new JLabel("Player"+(playerID+1)));
		panel.setSize(80,400);
		return panel;
	}
}
